package com.cms.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.cms.common.CmsClassUtils;
import com.cms.common.CmsContext;
import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;

public class BaseController {

	/**
	 * 发送文本。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 * @throws IOException 
	 */
	public  void renderText(HttpServletResponse response, String text) throws IOException {
		render(response, "text/plain;charset=UTF-8", text);
	}

	/**
	 * 发送json。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 * @throws IOException 
	 */
	public   void renderJson(HttpServletResponse response, String text) throws IOException {
		render(response, "application/json;charset=UTF-8", text);
	}

	/**
	 * 发送xml。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 * @throws IOException 
	 */
	public   void renderXml(HttpServletResponse response, String text) throws IOException {
		render(response, "text/xml;charset=UTF-8", text);
	}

	/**
	 * 发送内容。使用UTF-8编码。
	 * 
	 * @param response
	 * @param contentType
	 * @param text
	 * @throws IOException 
	 */
	public  void render(HttpServletResponse response, String contentType, String text) throws IOException  
	{
		response.setContentType(contentType);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
	    response.getWriter().write(text);
		response.getWriter().flush();
	}
	
	/***
	 * 获取当前的登录用户
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public TrAdminUser getLoginUser(HttpServletRequest req)throws Exception
	{
		 HttpSession hs=req.getSession(false);
		 TrAdminUser tr =(TrAdminUser)hs.getAttribute(CmsConstant.CURRENT_USER);
	     return tr;
	}
	
	
	
	/***
	 * 获取所有request的参数 构建一个map结构数据
	 * @param req
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> buildQueryMap(HttpServletRequest req)throws Exception
	{
		Map<String,Object>result = new HashMap<String, Object>();
		String method = req.getMethod();
		Enumeration<String>en=  req.getParameterNames();
		while(en.hasMoreElements())
		{ 
			 String key = en.nextElement();
			 if("POST".equals(method))
			 {
				 result.put(key, req.getParameter(key));
			 }
			 else
			 {
				 result.put(key, URLDecoder.decode(req.getParameter(key), "utf-8"));
			 }
		}
		return result;
	}
	
	/***
	 * 
	 * @param req
	 * @param cl
	 * @throws Exception
	 */
	public void export(HttpServletRequest req,String prefix,Object obj)throws Exception
	{
		String method = req.getMethod();
		Enumeration<String>en=  req.getParameterNames();
		Class cl = obj.getClass();
		java.lang.reflect.Field field =null;
		String value = null;
		while(en.hasMoreElements())
		{ 
			 String key = en.nextElement();
			 if(key.startsWith(prefix))
			 { 
			   field =CmsClassUtils.getField(cl,key.substring(key.indexOf("*")+1)); 
			   if(field!=null)
			   { 
			    field.setAccessible(true);
			   if("POST".equals(method))
			   {
				   value = req.getParameter(key);
			   }
			   else
			   { 
				   value=URLDecoder.decode(req.getParameter(key), "utf-8");
			   }
			   
			   field.set(obj, convertResult(field.getType(),value));
			   field.setAccessible(false);
			   }
			 }
		}
	}
	
	
	/***
	 * 
	 * @param cl
	 * @param src
	 * @return
	 */
	private Object convertResult(Class cl,String src)throws Exception
	{
		Object result = null;
		if(cl==Double.class||cl==double.class)
		{
		   return StringUtils.str2Double(src);
		}
		else if(cl==Integer.class||cl==int.class)
		{
			return StringUtils.str2Int(src);
		}
		else if(cl==Long.class||cl==long.class)
		{
			return StringUtils.str2Long(src);
		}
		else if(cl==Date.class)
		{
			 result = new SimpleDateFormat("yyyy-MM-dd").parse(src);
			 return result;
		}
		return src;
	}
	
	/***
	 *  获取当前作业的作业代码
	 * @return
	 */
	public String getActivityCode()
	{
		 String act= CmsContext.getActivityCode();
		 return act;
	}
}
